package com.saudidentalapp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.saudiapp.webservice.Constants;
import com.saudiapp.webservice.Utilities;
import com.saudiapp.webservice.Utilities.DialogListener;
import com.saudidentalapp.datamodels.Program;

public class ActivityMySchedule extends Activity implements OnItemClickListener
{
	
	
	ListView lstPrograms;
	Utilities utilities;
	ActivityMySchedule activity;
	TextView txtDataStatus;
	
	ProgrameAdapter adapter;
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my_schedule);
		lstPrograms=(ListView)findViewById(R.id.xlstPrograms);
		txtDataStatus=(TextView)findViewById(R.id.xtxtDataStatus);
	
		
		
		
		ImageView imgBack=(ImageView)findViewById(R.id.xbtnVwBack);
		imgBack.setOnClickListener(new OnClickListener() 
		{
			
			@Override
			public void onClick(View arg0) 
			{
				finish();
				startActivity(new Intent(activity,TabActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
				
			}
		});
	}

	
	@Override
	protected void onResume() 
	{
		// TODO Auto-generated method stub
		super.onResume();
		setView();
	}
	private void setView() 
	{
		utilities=Utilities.getInstance(ActivityMySchedule.this);
		if(!Constants.MySchedules.isEmpty())
		{
			//adapter=new ProgrameAdapter(ActivityMySchedule.this, R.layout.custom_programs_row,Constants.MySchedules);
			if(adapter!=null)
			{
				lstPrograms.setAdapter(adapter);
				lstPrograms.setVisibility(View.VISIBLE);
				lstPrograms.setOnItemClickListener(this);
				txtDataStatus.setVisibility(View.GONE);
			}
		}
		else
		{
			txtDataStatus.setVisibility(View.VISIBLE);
		}
		
			
		
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3)
	{
		final Program program=(Program)arg0.getAdapter().getItem(position);	
		
		utilities.showYesNoDialog("Do you want to remove this program from schedule?","Remove","Cancel");
		utilities.setDialogListener(new DialogListener() {
			
			@Override
			public void OnYesButtonClicked() 
			{
				if(isProgramInList(program))
				{
					if(index!=-1)
						Constants.MySchedules.remove(index);
					   utilities.showToast("Program successfully removed from schedules..");
					   adapter.notifyDataSetChanged();
				}
				
				
				
			}
			
			@Override
			public void OnNoButtonClicked() {
				// TODO Auto-generated method stub
				
			}
		});
	}
	
	
	
	int index=-1;
	boolean isProgramInList(Program program)
	{
		index=-1;
		boolean result=false;
		for(int i=0;i<Constants.MySchedules.size();i++)
		{
			if(Constants.MySchedules.get(i).getID()==program.getID())
			{
				result=true;
				index=i;
				break;
			}
			else
			{
				result=false;
			}
		}
		
		return result;
	}
}
